package com.njrsrc.android.app;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.njrsrc.R;
import com.njrsrc.android.library.vpi.BaseSampleActivity;
import com.njrsrc.android.views.TitleBar;
import com.njrsrc.common.utils.Helpers;

/**
 * 找工作查询页面
 * 
 * @author Administrator
 */
public class FindJobActivity extends BaseSampleActivity implements OnClickListener {
	private TitleBar mBar;
	private RelativeLayout find_job_area_layout;
	private RelativeLayout find_job_industry_layout;
	private RelativeLayout find_job_position_layout;
	private ImageView searchImageView;
	private TextView areaTextView, industryTextView, positionTextView;
	private EditText keyWordEditText;
	private String keyWord;
	private String area_code = "";
	private String industry_code = "";
	private String position_code = "";
	private String areaName;
	private String industryName;
	private String positionName;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_find_job);
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	private void initView() {
		mBar = (TitleBar) findViewById(R.id.activity_title_bar);
		mBar.setTitleText("找工作");
		mBar.back.setOnClickListener(this);
		find_job_area_layout = (RelativeLayout) findViewById(R.id.find_job_area_layout);
		find_job_industry_layout = (RelativeLayout) findViewById(R.id.find_job_industry_layout);
		find_job_position_layout = (RelativeLayout) findViewById(R.id.find_job_position_layout);
		searchImageView = (ImageView) findViewById(R.id.find_job_search_btn);
		find_job_area_layout.setOnClickListener(this);
		find_job_industry_layout.setOnClickListener(this);
		find_job_position_layout.setOnClickListener(this);
		searchImageView.setOnClickListener(this);
		areaTextView = (TextView) findViewById(R.id.find_job_area_tv);
		industryTextView = (TextView) findViewById(R.id.find_job_industry_tv);
		positionTextView = (TextView) findViewById(R.id.find_job_position_tv);
		keyWordEditText = (EditText) findViewById(R.id.key_word);

		findViewById(R.id.clear).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 清空历史
				Editor editor = preferences.edit();
				for (int i = 1; i <= 100; i++) {
					// 不存在
					editor.putString("area_code" + i, "");
					editor.putString("industry_code" + i, "");
					editor.putString("position_code" + i, "");
					editor.putString("areaName" + i, "");
					editor.putString("industryName" + i, "");
					editor.putString("positionName" + i, "");
					editor.commit();
				}
				AppApplication.clear();
				areaTextView.setText("");
				industryTextView.setText("");
				positionTextView.setText("");
				layout01.setVisibility(View.GONE);
				layout02.setVisibility(View.GONE);
				layout03.setVisibility(View.GONE);
				layout04.setVisibility(View.GONE);
				layout05.setVisibility(View.GONE);
			}
		});

		layout01 = (RelativeLayout) findViewById(R.id.search_layout01);
		layout02 = (RelativeLayout) findViewById(R.id.search_layout02);
		layout03 = (RelativeLayout) findViewById(R.id.search_layout03);
		layout04 = (RelativeLayout) findViewById(R.id.search_layout04);
		layout05 = (RelativeLayout) findViewById(R.id.search_layout05);

		searchView01 = (TextView) findViewById(R.id.search_01);
		searchView02 = (TextView) findViewById(R.id.search_02);
		searchView03 = (TextView) findViewById(R.id.search_03);
		searchView04 = (TextView) findViewById(R.id.search_04);
		searchView05 = (TextView) findViewById(R.id.search_05);

		getHistory();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tb_ibtn_back:
			AppApplication.clear();
			finish();
			break;
		case R.id.find_job_area_layout:
			// 选择地区
			Intent areaIntent = new Intent();
			areaIntent.setClass(FindJobActivity.this, SelectAreaActivity.class);
			startActivityForResult(areaIntent, 0);
			break;
		case R.id.find_job_industry_layout:
			// 选择行业
			Intent industryIntent = new Intent();
			industryIntent.setClass(FindJobActivity.this, SelectIndustryActivity.class);
			startActivityForResult(industryIntent, 0);
			break;
		case R.id.find_job_position_layout:
			// 选择职位
			Intent positionIntent = new Intent();
			positionIntent.setClass(FindJobActivity.this, SelectPositionActivity.class);
			startActivityForResult(positionIntent, 0);
			break;
		case R.id.find_job_search_btn:
			// 搜索
			keyWord = keyWordEditText.getText().toString();
			if (keyWord.equals("") && area_code.equals("") && industry_code.equals("") && position_code.equals("")) {
				Helpers.showToast(FindJobActivity.this, "请至少选择一个搜索条件", Toast.LENGTH_SHORT);
				return;
			}
			Intent searchIntent = new Intent();
			searchIntent.setClass(FindJobActivity.this, JobCollectActivity.class);
			searchIntent.putExtra("from", "search");
			searchIntent.putExtra("area_code", area_code);
			searchIntent.putExtra("industry_code", industry_code);
			searchIntent.putExtra("position_code", position_code);
			searchIntent.putExtra("keyWord", keyWord);

			saveHistory();
			getHistory();

			startActivity(searchIntent);
			// AppApplication.clear();
			break;
		default:
			break;
		}
	}

	private RelativeLayout layout01, layout02, layout03, layout04, layout05;
	private TextView searchView01, searchView02, searchView03, searchView04, searchView05;
	private SharedPreferences preferences;

	private void saveHistory() {
		preferences = getSharedPreferences("SearchHistory", MODE_PRIVATE);
		Editor editor = preferences.edit();
		for (int i = 1; i <= 100; i++) {
			if (!preferences.getString("area_code" + i, "").equals("")
					|| !preferences.getString("industry_code" + i, "").equals("")
					|| !preferences.getString("position_code" + i, "").equals("")) {
				if (preferences.getString("area_code" + i, "").equals(area_code)
						&& preferences.getString("industry_code" + i, "").equals(industry_code)
						&& preferences.getString("position_code" + i, "").equals(position_code)) {
					// 重复
					return;
				}
			} else {
				// 不存在
				editor.putString("area_code" + i, area_code);
				editor.putString("industry_code" + i, industry_code);
				editor.putString("position_code" + i, position_code);
				editor.putString("areaName" + i, areaName);
				editor.putString("industryName" + i, industryName);
				editor.putString("positionName" + i, positionName);
				editor.commit();
				break;
			}
		}
	}

	private void getHistory() {
		preferences = getSharedPreferences("SearchHistory", MODE_PRIVATE);
		int from = 0;
		for (int i = 100; i >= 1; i--) {
			if (!preferences.getString("area_code" + i, "").equals("")
					|| !preferences.getString("industry_code" + i, "").equals("")
					|| !preferences.getString("position_code" + i, "").equals("")) {
				from = i;
				break;
			}
		}
		for (int i = 0; i < 6; i++, from--) {
			System.out.println("from == " + from);
			String area_code = preferences.getString("area_code" + from, "");
			String industry_code = preferences.getString("industry_code" + from, "");
			String position_code = preferences.getString("position_code" + from, "");
			String areaName = preferences.getString("areaName" + from, "");
			String industryName = preferences.getString("industryName" + from, "");
			String positionName = preferences.getString("positionName" + from, "");
			String showText = areaName;
			if (areaName.equals("")) {
				showText = industryName;
				if (industryName.equals("")) {
					showText = positionName;
				} else {
					if (!positionName.equals("")) {
						showText = showText + "+ " + positionName;
					}
				}
			} else {
				if (!industryName.equals("")) {
					showText = showText + " + " + industryName;
				}
				if (!positionName.equals("")) {
					showText = showText + " + " + positionName;
				}
			}
			switch (i) {
			case -1:
				// if (!area_code.equals("") || !industry_code.equals("") ||
				// !position_code.equals("")) {
				// areaTextView.setText(areaName);
				// industryTextView.setText(industryName);
				// positionTextView.setText(positionName);
				// this.area_code = area_code;
				// this.industry_code = industry_code;
				// this.position_code = position_code;
				// this.areaName = areaName;
				// this.industryName = industryName;
				// this.positionName = positionName;
				// if (!area_code.equals("")) {
				// AreaInfo areaInfo = new AreaInfo(areaName, area_code);
				// if (!AppApplication.chooseArea.contains(areaInfo)) {
				// AppApplication.chooseArea.add(areaInfo);
				// }
				// }
				// if (!industry_code.equals("")) {
				// IndustryInfo industryInfo = new IndustryInfo(industryName,
				// industry_code);
				// if (!AppApplication.chooseIndustry.contains(industryInfo)) {
				// AppApplication.chooseIndustry.add(industryInfo);
				// }
				// }
				// if (!position_code.equals("")) {
				// PositionInfo positionInfo = new PositionInfo(positionName,
				// position_code);
				// if (!AppApplication.choosePosition.contains(positionInfo)) {
				// AppApplication.choosePosition.add(positionInfo);
				// }
				// }
				// }
				break;
			case 0:
				if (!area_code.equals("") || !industry_code.equals("") || !position_code.equals("")) {
					layout01.setVisibility(View.VISIBLE);
					searchView01.setText(showText);
					click(layout01, from);
				}
				break;
			case 1:
				if (!area_code.equals("") || !industry_code.equals("") || !position_code.equals("")) {
					layout02.setVisibility(View.VISIBLE);
					searchView02.setText(showText);
					click(layout02, from);
				}
				break;
			case 2:
				if (!area_code.equals("") || !industry_code.equals("") || !position_code.equals("")) {
					layout03.setVisibility(View.VISIBLE);
					searchView03.setText(showText);
					click(layout03, from);
				}
				break;
			case 3:
				if (!area_code.equals("") || !industry_code.equals("") || !position_code.equals("")) {
					layout04.setVisibility(View.VISIBLE);
					searchView04.setText(showText);
					click(layout04, from);
				}
				break;
			case 4:
				if (!area_code.equals("") || !industry_code.equals("") || !position_code.equals("")) {
					layout05.setVisibility(View.VISIBLE);
					searchView05.setText(showText);
					click(layout05, from);
				}
				break;
			default:
				break;
			}
		}
	}

	private void click(View view, final int from) {
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String area_code = preferences.getString("area_code" + from, "");
				String industry_code = preferences.getString("industry_code" + from, "");
				String position_code = preferences.getString("position_code" + from, "");
				// String areaName = preferences.getString("areaName" + from,
				// "");
				// String industryName = preferences.getString("industryName" +
				// from, "");
				// String positionName = preferences.getString("positionName" +
				// from, "");

				Intent searchIntent = new Intent();
				searchIntent.setClass(FindJobActivity.this, JobCollectActivity.class);
				searchIntent.putExtra("from", "search");
				searchIntent.putExtra("area_code", area_code);
				searchIntent.putExtra("industry_code", industry_code);
				searchIntent.putExtra("position_code", position_code);
				searchIntent.putExtra("keyWord", "");

				startActivity(searchIntent);
				// AppApplication.clear();
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == 1) {
			StringBuffer areaBuffer = new StringBuffer();
			StringBuffer industryBuffer = new StringBuffer();
			StringBuffer positionBuffer = new StringBuffer();
			StringBuffer area_codeBuffer = new StringBuffer();
			StringBuffer industry_codeBuffer = new StringBuffer();
			StringBuffer position_codeBuffer = new StringBuffer();
			for (int i = 0; i < AppApplication.chooseArea.size(); i++) {
				if (i == AppApplication.chooseArea.size() - 1) {
					areaBuffer.append(AppApplication.chooseArea.get(i).getName());
					area_codeBuffer.append(AppApplication.chooseArea.get(i).getCode());
				} else {
					areaBuffer.append(AppApplication.chooseArea.get(i).getName()).append("+");
					area_codeBuffer.append(AppApplication.chooseArea.get(i).getCode()).append(",");
				}
			}
			area_code = area_codeBuffer.toString();
			areaName = areaBuffer.toString();
			areaTextView.setText(areaName);
			for (int i = 0; i < AppApplication.chooseIndustry.size(); i++) {
				if (i == AppApplication.chooseIndustry.size() - 1) {
					industryBuffer.append(AppApplication.chooseIndustry.get(i).getName());
					industry_codeBuffer.append(AppApplication.chooseIndustry.get(i).getCode());
				} else {
					industryBuffer.append(AppApplication.chooseIndustry.get(i).getName()).append("+");
					industry_codeBuffer.append(AppApplication.chooseIndustry.get(i).getCode()).append(",");
				}
			}
			industry_code = industry_codeBuffer.toString();
			industryName = industryBuffer.toString();
			industryTextView.setText(industryName);
			for (int i = 0; i < AppApplication.choosePosition.size(); i++) {
				if (i == AppApplication.choosePosition.size() - 1) {
					positionBuffer.append(AppApplication.choosePosition.get(i).getName());
					position_codeBuffer.append(AppApplication.choosePosition.get(i).getCode());
				} else {
					positionBuffer.append(AppApplication.choosePosition.get(i).getName()).append("+");
					position_codeBuffer.append(AppApplication.choosePosition.get(i).getCode()).append(",");
				}
			}
			position_code = position_codeBuffer.toString();
			positionName = positionBuffer.toString();
			positionTextView.setText(positionName);
		}
	}
}
